#!/usr/bin/env python

import wsgiref.handlers
from google.appengine.ext import webapp
from google.appengine.api.labs import taskqueue

from google.appengine.ext import db
from schema import Fetch

import logging

class FetchCompresser(webapp.RequestHandler):
  def post(self):
    fetch_key = self.request.get("fetch_key")
    logging.debug("I'm going to try to compress fetch %s." % fetch_key)
    encode = self.request.get("encode")
    fetch = Fetch.get(db.Key(fetch_key))
    if fetch.encode != encode:
      uncompressed_content = fetch.content
      fetch.encode = encode
      fetch.content = uncompressed_content
      fetch.put()

def main():
  logging.getLogger().setLevel(logging.DEBUG)
  wsgiref.handlers.CGIHandler().run(webapp.WSGIApplication([
    ('/compress-fetch', FetchCompresser),
  ]))

if __name__ == '__main__':
  main()
